#! /usr/bin/python
# - coding: utf-8 -*-

# Copyright (c) 2007-2008 Renato Chencarek. All rights reserved.
#
# Author: Renato Chencarek <renato.chencarek@gmail.com>
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import os
import ConfigParser
from syslog import openlog, closelog, syslog, LOG_ERR, LOG_INFO, LOG_DAEMON

def read_config_file(path, sections):
    config = ConfigParser.ConfigParser()
    config.optionxform = str
    config.read([path,])
    mconf = {}

    for section in sections:
        for k, v in config.items(section):
            mconf[k] = v

    return mconf

def change_owner(folder, user):
    os.system('chown -R %s "%s"' % (user, folder))

def mkdirs(path):
    os.system('mkdir -p "%s"' % path)

def print_std(message):
    print message

def log(message):
    syslog(LOG_INFO, message)

def log_and_print(message):
    syslog(LOG_INFO, message)
    print message

def start_log(name, print_std):
    openlog(name, 0, LOG_DAEMON)
    if print_std:
        return log_and_print
    else:
        return log

def end_log():
    closelog()
